#! /bin/bash

# check whether some essential packages exist.
# if not installed, install them from software repos.
# essential packages:
# gcc g++ gdb make
# exuberant-ctags cscope
# ftp tree wget curl lynx 
# openssh-server openssh-client
# mysql-server mysql-client
# vim git svn rcs sed gawk
# chromium-browser
# samba gnupg

MY_PROGRAM_PACKS="gcc g++ gdb make exuberant-ctags cscope vim git"
MY_UTILITY_PACKS="ftp tree wget curl lynx sed gawk chromium-browser iftop htop gnupg"
MY_MANAGE_PACKS="openssh-server openssh-client mysql-server mysql-client samba smbfs"

MY_PACKS="$MY_PROGRAM_PACKS $MY_UTILITY_PACKS $MY_MANAGE_PACKS"

STATUS_1=" installed"
STATUS_2=" not installed"

# for statistics result
INIT_INS_PACKS=
NEW_INS_PACKS=
FAIL_INS_PACKS=


for pack in $MY_PACKS
do
	echo  "checking for $pack ..."

	status=`aptitude show $pack|grep State:|cut -d ":" -f 2`
	version=`aptitude show $pack|grep Version:|cut -d ":" -f 2-`

	if [ "$status" = "$STATUS_1" ]
	then
		INIT_INS_PACKS="$INIT_INS_PACKS $pack"
		echo "$pack is $status,version is $version"
	else
		echo "$pack is $status, start to install $version ..."
		echo 'y'|aptitude install $pack
		if [ $? -eq 0 ]
		then
			NEW_INS_PACKS="$NEW_INS_PACKS $pack"
			echo "install $pack ($version) successfully."
		else
			FAIL_INS_PACKS="$FAIL_INS_PACKS $pack"
			echo "install $pack ($version) failed."
		fi
	fi
	echo "checking $pack done."
done

echo "the checking packages result:"
echo "    initial installed packages: $INIT_INS_PACKS "
echo "    new installed packages: $NEW_INS_PACKS "
echo "    failed installed packages: $FAIL_INS_PACKS "

exit 0

